//
//  UIView+Helper.m
//  Doumi
//
//  Created by reyzhang on 2021/3/17.
//  Copyright © 2021 myjs. All rights reserved.
//

#import "UIView+Helper.h"

@implementation UIView (Helper)

- (void)drawLineWithRect:(CGRect)rect
{
    [self drawLineWithRect:rect  lineColor:[UIColor colorWithRed:238/255.0 green:238/255.0 blue:238/255.0 alpha:1]];
}

- (void)drawLineWithRect:(CGRect)rect lineColor:(UIColor *)color {
    CAShapeLayer *layer = [self getShapeLayerWithRect:rect lineColor:color];
    [self.layer addSublayer:layer];
}

- (CAShapeLayer *)getShapeLayerWithRect:(CGRect)rect lineColor:(UIColor *)color{
    CAShapeLayer *layer = [CAShapeLayer layer];
    layer.fillColor = color.CGColor;
    UIBezierPath  *path = [UIBezierPath bezierPathWithRect:rect];
    layer.path = path.CGPath;
    return layer;
}




+ (UINib *)nib {
    return [UINib nibWithNibName:NSStringFromClass([self class]) bundle:nil];
}

+ (instancetype)loadFromNib {
    return [[NSBundle mainBundle] loadNibNamed:[self identifier] owner:nil options:nil].lastObject;
}
+ (NSString *)identifier {
    return NSStringFromClass([self class]);
}


- (void)addRoundingCorners:(UIRectCorner)corners cornerRadii:(CGSize)cornerRadii {
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:corners cornerRadii:cornerRadii];
    
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.path = maskPath.CGPath;
    maskLayer.frame = self.bounds;
    self.layer.mask = maskLayer;
}

//加线性渐变色
- (void)addLineGradientLayerWithColors:(NSArray *)colors
                         locations:(NSArray<NSNumber *> *)locations
                        startPoint:(CGPoint)startPoint
                          endPoint:(CGPoint)endPoint {
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    NSMutableArray *_colors = @[].mutableCopy;
    
    //convert color to cgcolor
    for (UIColor *color in colors) {
        [_colors addObject:(__bridge id)color.CGColor];
    }
    gradientLayer.colors = _colors;
    gradientLayer.locations = locations;
    gradientLayer.startPoint = startPoint;
    gradientLayer.endPoint = endPoint;
    gradientLayer.frame = self.bounds;
    [self.layer addSublayer:gradientLayer];
}

//加阴影效果
- (void)addShadowEffectWithColor:(UIColor *)shadowColor
                          offset:(CGSize)shadowOffset
                         opacity:(CGFloat)shadowOpacity
                          radius:(CGFloat)shadowRadius {
    CALayer *layer = self.layer;
    layer.masksToBounds = NO;
//    layer.borderWidth = 0.0000000001;
//    UIBezierPath *path = [UIBezierPath bezierPathWithRect:layer.bounds];
//    layer.shadowPath = path.CGPath;
    layer.shadowColor = shadowColor.CGColor;
    layer.shadowOffset = shadowOffset;
    layer.shadowOpacity = shadowOpacity;
    layer.shadowRadius = shadowRadius;
    
}

@end
